package com.leyou.goods.service;

import com.leyou.goods.client.BrandClient;
import com.leyou.goods.client.CategoryClient;
import com.leyou.goods.client.GoodsClient;
import com.leyou.goods.client.SpecificationClient;
import com.leyou.item.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CHERRY
 * @date 2018-09-12-11:47
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private SpecificationClient specificationClient;

    /**
     * 根据spuId 查询spu的详细信息
     *
     * @param id spuId
     * @return
     */
    public Map<String, Object> loadItemModel(Long id) {
        Map<String, Object> map = new HashMap<>();

        //查询spu
        Spu spu = this.goodsClient.querySpuById(id);

        //查询品牌
        Brand brand = this.brandClient.queryBrandByBid(spu.getBrandId());

        //查询分类
        List<Category> categories = this.categoryClient.queryAllCategoryByCid3(spu.getCid3());

        //查询组
        List<SpecGroup> specGroups = this.specificationClient.querySpecGroupAndSpecParamsByCid(spu.getCid3());

        //查询sku
        List<Sku> skus = this.goodsClient.querySKu(id);

        //查询spuDetail
        SpuDetail spuDetail = this.goodsClient.querySpuDetail(id);

        //查询特殊参数
        List<SpecParam> specParams = this.specificationClient.querySpecParamByCid(spu.getCid3(), null, null, false);

        Map<Long, String> specMap = new HashMap<>();
        specParams.forEach(specParam -> {
            specMap.put(specParam.getId(), specParam.getName());
        });

        map.put("categories", categories);
        map.put("brand", brand);
        map.put("spu", spu);
        map.put("skus", skus);
        map.put("spuDetail", spuDetail);
        map.put("groups", specGroups);
        map.put("specs", specMap);

        return map;
    }


}
